#include "copycat.h"

void HandReorder( CTree &tree, CUString par_label, CUString c1_label, CUString c2_label )
{
	int ls[NUM_MAX_NODE];
	int s=-1;

	tree.GetTraverseList( ls, s );
	for( int i=0; i<s; i++ )	{
		CNode &n = tree.node[ ls[i] ];

		if( n.label != par_label )	continue;
		if( n.num_child != 2 )	continue;

		CNode &c1 = tree.node[ n.child[0] ];
		CNode &c2 = tree.node[ n.child[1] ];

		if( c1.label != c1_label )	continue;
		if( c2.label != c2_label )	continue;

		int tmp = n.child[0];
		n.child[0] = n.child[1];
		n.child[1] = tmp;
	}

}


void HandInsert1( CTree &tree )
{
	int ls[NUM_MAX_NODE];
	int s=-1;

	tree.GetTraverseList( ls, s );
	for( int i=0; i<s; i++ )	{
		CNode &n = tree.node[ ls[i] ];

		if( n.label != "NP" )	continue;
		if( n.num_child != 2 )	continue;

		CNode &c1 = tree.node[ n.child[0] ];
		CNode &c2 = tree.node[ n.child[1] ];

		if( c1.label != "NP" )	continue;
		if( c2.label != "PP" )	continue;

		int pos_id = tree.num_node++;
		CNode &pos_node = tree.node[ pos_id ]; pos_node.id = pos_id;

		int lex_id = tree.num_node++;
		CNode &lex_node = tree.node[ lex_id ]; lex_node.id = lex_id;

		n.num_child++;
		n.child[2] = n.child[1];
		n.child[1] = pos_id;

		pos_node.parent = n.id;
		pos_node.num_child = 1;
		pos_node.child[0] = lex_id;
		pos_node.label = "JKG";
		
		lex_node.parent = pos_id;
		lex_node.num_child = 0;
		lex_node.label = "의";
	}

}

void HandReorderInsert( CTree &tree )
{
	int ls[NUM_MAX_NODE];
	int s=-1;

	tree.GetTraverseList( ls, s );
	for( int i=0; i<s; i++ )	{
		CNode &n = tree.node[ ls[i] ];

		if( n.label != "NP" )	continue;
		if( n.num_child != 2 )	continue;

		CNode &c1 = tree.node[ n.child[0] ];
		CNode &c2 = tree.node[ n.child[1] ];

		if( c1.label != "NP" )	continue;
		if( c2.label != "PP" )	continue;

		int pos_id = tree.num_node++;
		CNode &pos_node = tree.node[ pos_id ]; pos_node.id = pos_id;

		int lex_id = tree.num_node++;
		CNode &lex_node = tree.node[ lex_id ]; lex_node.id = lex_id;

		n.num_child++;
		n.child[2] = n.child[0];
		n.child[0] = n.child[1];
		n.child[1] = pos_id;

		pos_node.parent = n.id;
		pos_node.num_child = 1;
		pos_node.child[0] = lex_id;
		pos_node.label = "JKG";
		
		lex_node.parent = pos_id;
		lex_node.num_child = 0;
		lex_node.label = "의";
	}

}
int main( int argc, char *argv[] )
{
	if( argc != 2 )	{
		fprintf( stderr, "usage: %s  src-prefix\n", argv[0] );
		return 1;
	}

	CUString fn = CUString( argv[1] ) + CUString(".parse" );
	CUTextFile parse_file( fn, "r" );
	if( parse_file.CheckOpen() == false )	return 1;

	CUString fn2 = CUString( argv[1] ) + CUString("-hrule.parse" );
	CUTextFile out_file( fn2, "w" );

	CUString fn3 = CUString( argv[1] ) + CUString("-hrule.SP" );
	CUTextFile out_file2( fn3, "w" );
	
	int num_sent = 0;
	while(1)	{
		CUString line;
		if( parse_file.ReadLine( line ) == false )	break;
		line.Replace("\r", "" );
		num_sent++;

		if( num_sent % 1000 == 0 )		fprintf( stderr, "." );
		else if( num_sent % 10000 == 0 )		fprintf( stderr, "\n" );


		CTree tree;
		if( tree.LoadTree( line ) == false )	return 1;

		HandReorder( tree, "NP", "NP", "PP" ); 		// 16.4 -> 17.29
		HandReorder( tree, "VP", "MD", "VP" ); 		// 16.4 -> 16.82
		HandReorder( tree, "SBAR", "IN", "S" ); 	// 16.4 -> 16.74
		HandReorder( tree, "VP", "VB", "NP" ); 		// 16.4 -> 16.90
		HandReorder( tree, "VP", "VBN", "PP" ); 	// 16.4 -> 16.71
		// all 16.4 -> 
		//HandInsert1( tree );
		/*
		HandReorderInsert( tree );
		*/

		CUString str; tree.PrintStrRecurr( 0, str); str.Trim(" ");
		fprintf( out_file.GetFP(), "%s\n", str.GetStr() );
		tree.PrintLexPos2File( out_file2 );
	}

	fprintf( stderr, "\n" );
	fprintf( stderr, "%d sents\n", num_sent );


	return 0;
}

